package com.krm.codemanager.service;

import java.beans.IntrospectionException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.krm.codemanager.mapper.SystemMapper;
import com.krm.codemanager.model.SourceSystem;

import utils.CommonEntity;
import utils.ExcelBean;
import utils.ExcelUtils;

/**
 * @Title: ReportService.java
 * @Package com.krm.web.design.service
 * @Description: TODO(用一句话描述该文件做什么)
 * @author: yys
 * @date: 2018年6月11日 下午2:26:10
 * @version report V2.0
 */
@Service("systemService")
public class SystemService {

	@Resource
	private SystemMapper systemMapper;

	public PageInfo<utils.CommonEntity> queryPageInfo(Map<String, Object> params) {
		PageHelper.startPage(params);
		List<CommonEntity> list = systemMapper.pageInfo(params);
		return new PageInfo<CommonEntity>(list);
	}

	public int addCategory(SourceSystem sourceSystem) {
		return systemMapper.addCategory(sourceSystem);
	}

	public int updateCategory(SourceSystem sourceSystem) {
		return systemMapper.updateCategory(sourceSystem);
	}

	public int delCategory(Integer categoryId) {
		return systemMapper.delCategory(categoryId);
	}

	public int insertBatch(List<SourceSystem> list) {
		// TODO Auto-generated method stub
		return systemMapper.insertBatch(list);
	}

	public List<SourceSystem> entityList(Map<String, Object> params) {
		List<SourceSystem> entityList = new ArrayList<>();
		// TODO Auto-generated method stub
		List<CommonEntity> list = systemMapper.pageInfo(params);
		for (CommonEntity commonEntity : list) {
			entityList.add(JSON.toJavaObject(commonEntity, SourceSystem.class));
		}
		return entityList;
	}

	public List<SourceSystem> queryCategory(String createUser) {
		// TODO Auto-generated method stub
		return systemMapper.queryCategory(createUser);
	}

	public SourceSystem selectById(Integer categoryId) {
		return systemMapper.getCategoryByStdCatId(categoryId.toString());
	}

	public String uploadPayerCreditInfoExcel(InputStream in, MultipartFile file) throws Exception {
		// TODO Auto-generated method stub
		List<List<Object>> listob = ExcelUtils.getBankListByExcel(in, file.getOriginalFilename());
		// List<CategoryMaintain> creditInfoList = new ArrayList<CategoryMaintain>();
		int j = 0;
		int k = 0;
		for (int i = 0; i < listob.size(); i++) {
			List<Object> ob = listob.get(i);
			SourceSystem creditInfoBean = new SourceSystem();
			if (ob.get(0) != null && !ob.get(0).equals("")) {
				creditInfoBean.setSrcSys(String.valueOf(ob.get(0)));
			}
			if (ob.get(1) != null && !ob.get(1).equals("")) {
				creditInfoBean.setSrcSysName(String.valueOf(ob.get(1)));
			}
			if (ob.get(2) != null && !ob.get(2).equals("")) {
				creditInfoBean.setSrcCode(String.valueOf(ob.get(2)));
			}
			if (ob.get(3) != null && !ob.get(3).equals("")) {
				creditInfoBean.setSrcCodeName(String.valueOf(ob.get(3)));
			}
			if (ob.get(4) != null && !ob.get(4).equals("")) {
				creditInfoBean.setSrcCodeDesc(String.valueOf(ob.get(4)));
			}
			if (ob.get(5) != null && !ob.get(5).equals("")) {
				creditInfoBean.setCreateTime(String.valueOf(ob.get(5)));
			}
			if (ob.get(6) != null && !ob.get(6).equals("")) {
				creditInfoBean.setModefyTime(String.valueOf(ob.get(6)));
			}
			if (ob.get(7) != null && !ob.get(7).equals("")) {
				creditInfoBean.setCreatorName(String.valueOf(ob.get(7)));
			}
			if (ob.get(8) != null && !ob.get(8).equals("")) {
				creditInfoBean.setMaintainerName(String.valueOf(ob.get(8)));
			}
			SourceSystem onlyone = systemMapper.getOnlyone(creditInfoBean);
			if (onlyone != null) {
				j = j + 1;
			} else {
				systemMapper.addCategory(creditInfoBean);
				k = k + 1;
			}

			// creditInfoList.add(creditInfoBean);
		}
		String msg = "导入成功" + k + "条，重复数据" + j + "条";
		return msg;
	}

	public XSSFWorkbook exportPayerCreditInfoExcel(String user) {
		List<SourceSystem> creditInfoList = queryCategory(user);
		List<ExcelBean> ems = new ArrayList<>();
		Map<Integer, List<ExcelBean>> map = new LinkedHashMap<>();
		ems.add(new ExcelBean("源系统", "srcSys", 0));
		ems.add(new ExcelBean("源系统名称", "srcSysName", 0));
		ems.add(new ExcelBean("代码", "srcCode", 0));
		ems.add(new ExcelBean("代码名称", "srcCodeName", 0));
		ems.add(new ExcelBean("代码描述", "srcCodeDesc", 0));
		ems.add(new ExcelBean("创建时间", "createTime", 0));
		ems.add(new ExcelBean("修改时间", "modefyTime", 0));
		ems.add(new ExcelBean("创建人", "creatorName", 0));
		ems.add(new ExcelBean("维护人", "maintainerName", 0));
		map.put(0, ems);
		XSSFWorkbook book = null;
		try {
			book = ExcelUtils.createExcelFile(SourceSystem.class, creditInfoList, map, "源系统代码");
		} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException | ClassNotFoundException
				| IntrospectionException | ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return book;
	}

	public SourceSystem getOnlyone(SourceSystem sourceSystem) {
		// TODO Auto-generated method stub
		return systemMapper.getOnlyone(sourceSystem);
	}
}
